<?php
namespace Monkey;

/**
 * 请求类\Monkey\Request
 * @package    Monkey
 * @author     HuangYi
 * @copyright  Copyright (c) 2011-07-01——2013-03-30
 * @license    New BSD License
 * @version    $Id: \Monkey\Request.php 版本号 2013-03-30 $
 */
final class Request{
    /**
     * 获取$_ENV信息（environment）
     * @param string|array $name $_ENV的键值名称
     * @param string $defaultValue 当获取变量失败的时候返回该值,默认该值为null
     * @return string
     */
    public static function env($name = null, $defaultValue = null) {
        if($name === null) return $_ENV;
        if(is_array($name)) return self::_foreach($name,$_ENV);
        return (isset($_ENV[$name])) ? $_ENV[$name] : $defaultValue;
    }
    /**
     * 获取$_GET信息
     * @param string|array $name $_GET的键值名称
     * @param string $defaultValue 当获取变量失败的时候返回该值,默认该值为null
     * @return string
     */
    public static function get($name = null, $defaultValue = null) {
        if ($name === null) return $_GET;
        if(is_array($name)) return self::_foreach($name,$_GET);
        return (isset($_GET[$name])) ? $_GET[$name] : $defaultValue;
    }
    /**
     * 获取$_POST信息
     * @param string|array $name $_POST的键值名称
     * @param string $defaultValue 当获取变量失败的时候返回该值,默认该值为null
     * @return string
     */
    public static function post($name = null, $defaultValue = null) {
        if ($name === null) return $_POST;
        if(is_array($name)) return self::_foreach($name,$_POST);
        return (isset($_POST[$name])) ? $_POST[$name] : $defaultValue;
    }
    /**
     * 获取$_COOKIE信息
     * @param string|array $name COOKIE的键值名称
     * @param string $defaultValue 当获取变量失败的时候返回该值,默认该值为null
     * @return string
     */
    public static function cookie($name = null, $defaultValue = null) {
        if ($name === null) return $_COOKIE;
        if(is_array($name)) return self::_foreach($name,$_COOKIE);
        return (isset($_COOKIE[$name])) ? $_COOKIE[$name] : $defaultValue;
    }
    /**
     * 获取$_REQUEST信息,即 $_GET，$_POST 和 $_COOKIE 的合集中的信息
     * @param string|array $name $_REQUEST的键值名称
     * @param string $defaultValue 当获取变量失败的时候返回该值,默认该值为null
     * @return string
     */
    public static function request($name = null, $defaultValue = null) {
        if ($name === null) return $_REQUEST;
        if(is_array($name)) return self::_foreach($name,$_REQUEST);
        return (isset($_REQUEST[$name])) ? $_REQUEST[$name] : $defaultValue;
    }
    /**
     * 获取SERVICE信息
     * @param string|array $name SERVER的键值名称
     * @param string $defaultValue 当获取变量失败的时候返回该值,默认该值为null
     * @return string
     */
    public static function server($name = null, $defaultValue = null) {
        if ($name === null) return $_SERVER;
        if(is_array($name)) return self::_foreach($name,$_SERVER);
        return (isset($_SERVER[$name])) ? $_SERVER[$name] : $defaultValue;
    }
    private static function _foreach($names,&$data){
        $result = array();
        foreach ($names as $key => $value) {
            if(is_int($key)) $result[$value]= isset($data[$value]) ? $data[$value] : null;
            else $result[$key]=isset($data[$key]) ? $data[$key] : $value;
        }
        return $result;
    }
    /**
     * 返回访问IP
     * @return string
     */
    public static function ip(){
        return ip();
    }
    /**
     * 是否为命令行执行方式
     * @return bool
     */
    public static function isCmd(){
        return !isset($_SERVER['REMOTE_ADDR']);
    }
    /**
     * 是否是AJAX请求(只支持Jquery的AJAX)
     * @return bool
     */
    public static function isAjax(){
        return IS_AJAX;
    }
    /**
     * 是否是POST提交方式
     * @return bool
     */
    public static function isPost(){
        return strtolower($_SERVER['REQUEST_METHOD']) === 'post';
    }
    /**
     * 是否是GET提交方式
     * @return bool
     */
    public static function isGet(){
        return strtolower($_SERVER['REQUEST_METHOD']) === 'get';
    }
    /**
     * 是否是PUT提交方式
     * @return bool
     */
    public static function isPut(){
        return strtolower($_SERVER['REQUEST_METHOD']) === 'put';
    }
    /**
     * 是否是DELETE提交方式
     * @return bool
     */
    public static function isDelete(){
        return strtolower($_SERVER['REQUEST_METHOD']) === 'delete';
    }
    /**
     * 是否是GET提交方式
     * @return bool
     */
    public static function isHttps(){
        return strtolower($_SERVER['HTTPS']) === 'on';
    }
    /**
     * 判断常用的爬虫函数
     * @author Mer
     * @return boolean
     */
    public static function isSpider() {
        $agent   = strtolower($_SERVER['HTTP_USER_AGENT']);
        $spiders = array(
            '/baiduspider/',
            '/sohu-search/',
            '/googlebot/',
            '/yahoo! slurp;/',
            '/msnbot/',
            '/mediapartners-google/',
            '/scooter/',
            '/yahoo-mmcrawler/',
            '/fast-webcrawler/',
            '/yahoo-mmcrawler/',
            '/yahoo! slurp/',
            '/fast-webcrawler/',
            '/fast enterprise crawler/',
            '/grub-client-/',
            '/msiecrawler/',
            '/npbot/',
            '/nameprotect/i',
            '/zyborg/i',
            '/worio bot heritrix/i',
            '/ask jeeves/',
            '/libwww-perl/i',
            '/gigabot/i',
            '/bot@bot.bot/i',
            '/seznambot/i'
        );
        foreach($spiders as $c) {
            if(preg_match($c, $agent)) return $c;
        }
        return FALSE;
    }
    /**
     * 获取客户端浏览器的具体软件名
     * @access public
     * @return string
     */
    public static function browser() {
        $user_agent_info = htmlspecialchars($_SERVER['HTTP_USER_AGENT']);
        if(strpos($user_agent_info,'MSIE 9.0')) {
            return 'IE9';
        }else if(strpos($user_agent_info,'MSIE 8.0')) {
            return 'IE8';
        }else if(strpos($user_agent_info,'MSIE 7.0')) {
            return 'IE7';
        }else if(strpos($user_agent_info,'MSIE 6.0')) {
            return 'IE6';
        }else if(strpos($user_agent_info,'Firefox')) {
            return 'Firfox';
        }else if(strpos($user_agent_info,'Chrome')) {
            return 'Chrome';
        }else if(strpos($user_agent_info,'Opera')) {
            return 'Opera';
        }else if(strpos($user_agent_info,'Safari')) {
            return 'Safari';
        }else if(strpos($user_agent_info,'Elinks')) {
            return 'Elinks';
        }else if(strpos($user_agent_info,'OmniWeb')) {
            return 'OmniWeb';
        }else if(strpos($user_agent_info,'Links')) {
            return 'Links';
        }else if(strpos($user_agent_info,'Lynx')) {
            return 'Lynx';
        }else if(strpos($user_agent_info,'Arora')) {
            return 'Arora';
        }else if(strpos($user_agent_info,'Epiphany')) {
            return 'Epiphany';
        }else if(strpos($user_agent_info,'Konqueror')) {
            return 'Konqueror';
        }else if(strpos($user_agent_info,'EudoraWeb')) {
            return 'EudoraWeb';
        }else if(strpos($user_agent_info,'Minimo')) {
            return 'Minimo';
        }else if(strpos($user_agent_info,'NetFront')) {
            return 'NetFront';
        }else if(strpos($user_agent_info,'POLARIS')) {
            return 'Polaris';
        }else if(strpos($user_agent_info,'BlackBerry')) {
            return 'BlackBerry';
        }else if(strpos($user_agent_info,'Nokia')) {
            return 'Nokia';
        }else{
            return 'Others';
        }
    }
    /**
     * 获取客户端操作系统信息
     * @access public
     * @return string
     */
    public static function os() {
        $user_agent_info = htmlspecialchars($_SERVER['HTTP_USER_AGENT']);
        if(strpos($user_agent_info,'Windows NT 6.1')) {
            return 'Windows 7';
        }else if(strpos($user_agent_info,'Windows NT 6.0')) {
            return 'Windows Vista';
        }else if(strpos($user_agent_info,'Windows NT 5.2')) {
            return 'Windows 2003';
        }else if(strpos($user_agent_info,'Windows NT 5.1')) {
            return 'Windows XP';
        }else if(strpos($user_agent_info,'Windows NT 5.0')) {
            return 'Windows 2000';
        }else if(strpos($user_agent_info,'Windows ME')) {
            return 'Windows ME';
        }else if(strpos($user_agent_info,'PPC Mac OS X')) {
            return 'OS X PPC';
        }else if(strpos($user_agent_info,'Intel Mac OS X')) {
            return 'OS X Intel';
        }else if(strpos($user_agent_info,'Win98')) {
            return 'Windows 98';
        }else if(strpos($user_agent_info,'Win95')) {
            return 'Windows 95';
        }else if(strpos($user_agent_info,'WinNT4.0')) {
            return 'Windows NT4.0';
        }else if(strpos($user_agent_info,'Mac OS X Mach-O')) {
            return 'OS X Mach';
        }else if(strpos($user_agent_info,'Ubuntu')) {
            return 'Ubuntu';
        }else if(strpos($user_agent_info,'Debian')) {
            return 'Debian';
        }else if(strpos($user_agent_info,'AppleWebKit')) {
            return 'WebKit';
        }else if(strpos($user_agent_info,'Mint/8')) {
            return 'Mint 8';
        }else if(strpos($user_agent_info,'Minefield')) {
            return 'Minefield Alpha';
        }else if(strpos($user_agent_info,'gentoo')) {
            return 'Gentoo';
        }else if(strpos($user_agent_info,'Kubuntu')) {
            return 'Kubuntu';
        }else if(strpos($user_agent_info,'Slackware/13.0')) {
            return 'Slackware 13';
        }else if(strpos($user_agent_info,'Fedora')) {
            return 'Fedora';
        }else if(strpos($user_agent_info,'FreeBSD')) {
            return 'FreeBSD';
        }else if(strpos($user_agent_info,'SunOS')) {
            return 'SunOS';
        }else if(strpos($user_agent_info,'OpenBSD')) {
            return 'OpenBSD';
        }else if(strpos($user_agent_info,'NetBSD')) {
            return 'NetBSD';
        }else if(strpos($user_agent_info,'DragonFly')) {
            return 'DragonFly';
        }else if(strpos($user_agent_info,'IRIX')) {
            return 'IRIX';
        }else if(strpos($user_agent_info,'Windows CE')) {
            return 'Windows CE';
        }else if(strpos($user_agent_info,'PalmOS')) {
            return 'PalmOS';
        }else if(strpos($user_agent_info,'Linux')) {
            return 'Linux';
        }else if(strpos($user_agent_info,'DragonFly')) {
            return 'DragonFly';
        }else if(strpos($user_agent_info,'Android')) {
            return 'Android';
        }else if(strpos($user_agent_info,'Mac OS X')) {
            return 'Mac OS X';
        }else if(strpos($user_agent_info,'iPhone')) {
            return 'iPhone OS';
        }else if(strpos($user_agent_info,'Symbian OS')) {
            return 'Symbian';
        }else if(strpos($user_agent_info,'Symbian OS')) {
            return 'Symbian';
        }else if(strpos($user_agent_info,'SymbianOS')) {
            return 'SymbianOS';
        }else if(strpos($user_agent_info,'webOS')) {
            return 'webOS';
        }else if(strpos($user_agent_info,'PalmSource')) {
            return 'PalmSource';
        }else{
            return 'Others';
        }
    }
    /**
     * @static
     * 获取接受JS传递中文编码
     * 作者：Min
     * @param array|string $data
     * @return array|string
     */
    public static function jsUnescapeCn($data){
        if(is_array($data)) return array_map(__METHOD__ , $data);
        $ret = '';
        $len = strlen($data);
        for ($i = 0; $i < $len; $i++) {
            if ($data[$i] == '%' && $data[$i+1] == 'u') {
                $val = hexdec(substr($data, $i+2, 4));
                if ($val < 0x7f)
                    $ret .= chr($val);
                else if($val < 0x800)
                    $ret .= chr(0xc0|($val>>6))
                        .chr(0x80|($val&0x3f));
                else
                    $ret .= chr(0xe0|($val>>12))
                        .chr(0x80|(($val>>6)&0x3f))
                        .chr(0x80|($val&0x3f));
                $i += 5;
            } else if ($data[$i] == '%') {
                $ret .= urldecode(substr($data, $i, 3));
                $i += 2;
            }
            else $ret .= $data[$i];
        }
        return $ret;
    }
}